package com.ruoyi.rsikManage.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.rsikManage.domain.ReportApproval;
import com.ruoyi.rsikManage.service.ReportApprovalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RequestMapping("/reportApproval")
@RestController
public class ReportApprovalController extends BaseController {
    @Autowired
    ReportApprovalService reportApprovalService;

    @Autowired
    private TokenService tokenService;


    /**
     * 查询报备审批表
     */
    @GetMapping("list/v2")
    public TableDataInfo selectReportApprovalList(ReportApproval reportApproval,HttpServletRequest httpServletRequest) {
        startPage();
        // 获取当前登录用户的单位代码
        List<Long> unitCodes = tokenService.getLoginUserUnitCodes(httpServletRequest);
        reportApproval.setUnitCodes(unitCodes);
        List<ReportApproval> reportApprovals = reportApprovalService.selectReportApprovalList(reportApproval);

        return getDataTable(reportApprovals);
    }

    /**
     * 查询报备审批表
     * 此方法用于处理POST请求，接收一个包含查询条件的ReportApproval对象，
     * 并根据单位编码(unitCodes)过滤查询结果。
     * 如果请求体中的unitCodes为空，则尝试从登录用户的token中获取单位编码。
     */
    @PostMapping("list") // 使用@PostMapping注解指定此方法处理HTTP POST请求，并映射到URL路径"list"
    public TableDataInfo selectReportApprovalListForUnitCodes(@RequestBody ReportApproval reportApproval, // 通过@RequestBody注解将请求体中的JSON数据绑定到ReportApproval对象
                                                              HttpServletRequest httpServletRequest) { // 获取HttpServletRequest对象以访问HTTP请求信息

        List<Long> unitCodes = reportApproval.getUnitCodes(); // 从传入的reportApproval对象中获取单位编码列表
        // 如果传入的单位编码为空或未设置，则尝试从用户token中获取默认单位编码
        if (unitCodes == null || unitCodes.isEmpty()) {
            List<Long> defaultUnitCodes = tokenService.getLoginUserUnitCodes(httpServletRequest); // 调用tokenService获取当前登录用户的单位编码
            if (defaultUnitCodes != null) {
                unitCodes = defaultUnitCodes; // 使用从token中获取的单位编码
            }
        }
        reportApproval.setUnitCodes(unitCodes); // 更新reportApproval对象中的单位编码

        // 根据pageNum和pageSize启动分页，这两个参数通常来自于前端传来的分页信息
        startPage(reportApproval.getPageNum(), reportApproval.getPageSize());

        // 调用service层的方法查询符合条件的报备审批列表
        List<ReportApproval> reportApprovals = reportApprovalService.selectReportApprovalList(reportApproval);

        // 将查询结果封装成TableDataInfo对象返回给前端
        return getDataTable(reportApprovals);
    }
    /**
     * 获取报备信息审批详细信息
     */
    //@PreAuthorize("@ss.hasPermi('rsikManage:reportApproval:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult selectReportApprovalById(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(reportApprovalService.selectReportApprovalById(id));
    }

    /**
     * 新增报备审批表
     */
    @PostMapping("add")
    public AjaxResult addReportApproval(@RequestBody ReportApproval reportApproval,HttpServletRequest httpServletRequest) {
        Long unitCode = reportApproval.getUnitCode();
        // 获取当前登录用户的单位代码
        if (unitCode==null ){
            unitCode = tokenService.getLoginUserUnitCode(httpServletRequest);
            reportApproval.setUnitCode(unitCode);
        }
        return AjaxResult.success(reportApprovalService.addReportApproval(reportApproval));
    }
    /**
     * 修改报备审批表
     */
    @PutMapping("update")
    public AjaxResult updateReportApproval(@RequestBody ReportApproval reportApproval,HttpServletRequest httpServletRequest) {
        Long unitCode = reportApproval.getUnitCode();
        // 获取当前登录用户的单位代码
        if (unitCode==null ){
            unitCode = tokenService.getLoginUserUnitCode(httpServletRequest);
            reportApproval.setUnitCode(unitCode);
        }
        return AjaxResult.success(reportApprovalService.updateReportApproval(reportApproval));
    }

    /**
     * 删除报备审批表
     */


    /**
     * 删除报备审批表
     */
    @PutMapping("delete")
    public AjaxResult deleteReportApproval(@RequestBody ReportApproval reportApproval,HttpServletRequest httpServletRequest) {

        Long unitCode = reportApproval.getUnitCode();
        // 获取当前登录用户的单位代码
        if (unitCode==null ){
            unitCode = tokenService.getLoginUserUnitCode(httpServletRequest);
        }
        reportApproval.setUnitCode(unitCode);

        return AjaxResult.success(reportApprovalService.deleteReportApproval(reportApproval));
    }
    /**
     * 删除报备审批
     */
    //@PreAuthorize("@ss.hasPermi('rsikManage:reportApproval:remove')")
    //@Log(title = "报备信息审批", businessType = BusinessType.DELETE)
    @DeleteMapping("delete/{ids}")
    public AjaxResult deleteReportApprovalByIds(@PathVariable Integer[] ids)
    {
        return toAjax(reportApprovalService.deleteReportApprovalByIds(ids));
    }
    /**
     * 报备
     */

    /**
     * 审批
     */
}
